package com.socialuni.center.web.controller;

import com.socialuni.sdk.api.model.ResultRO;
import com.socialuni.center.web.factory.CenterUserFollowDetailROFactory;
import com.socialuni.center.web.model.RO.user.CenterUserFollowDetailRO;
import com.socialuni.center.web.serive.FollowService;
import com.socialuni.center.web.utils.CenterUserUtil;
import com.socialuni.sdk.constant.CommonStatus;
import com.socialuni.sdk.model.DO.FollowDO;
import com.socialuni.sdk.model.DO.user.UserDO;
import com.socialuni.sdk.model.QO.FollowAddVO;
import com.socialuni.sdk.repository.FollowRepository;
import com.socialuni.sdk.repository.UserFollowDetailRepository;
import com.socialuni.sdk.repository.UserRepository;
import com.socialuni.sdk.utils.UserUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("socialuni/follow")
public class FollowController {
    @Resource
    private FollowRepository followRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private FollowService followService;

    @Resource
    UserFollowDetailRepository userFollowDetailRepository;


    @PostMapping("queryUserFollows")
    public ResultRO<Map<String, List<CenterUserFollowDetailRO>>> queryUserFollows() {
        UserDO mineUser = CenterUserUtil.getMineUser();
        Map<String, List<CenterUserFollowDetailRO>> map = new HashMap<>();
        //查询他的关注
        List<FollowDO> followDOS = followRepository.findTop30ByUserIdAndStatusOrderByUpdateTimeDesc(mineUser.getId(), CommonStatus.enable);
        List<UserDO> userDOS = followDOS.stream().map(followDO -> UserUtils.get(followDO.getBeUserId())).collect(Collectors.toList());
        List<CenterUserFollowDetailRO> followUserVOS = CenterUserFollowDetailROFactory.newUsers(userDOS, mineUser);
        //查询他的粉丝
        List<FollowDO> fans = followRepository.findTop30ByBeUserIdAndStatusOrderByUpdateTimeDesc(mineUser.getId(), CommonStatus.enable);
        List<UserDO> fansUserDOS = fans.stream().map(followDO -> UserUtils.get(followDO.getUserId())).collect(Collectors.toList());
        List<CenterUserFollowDetailRO> fansUserVOS = CenterUserFollowDetailROFactory.newUsers(fansUserDOS, mineUser);
        map.put("follows", followUserVOS);
        map.put("fans", fansUserVOS);
        return new ResultRO<>(map);
    }


    @PostMapping("addFollow")
    public ResultRO<Void> addFollow(@RequestBody @Valid @NotNull FollowAddVO addVO) {
        followService.addFlow(addVO);
        return new ResultRO<>();
    }


    @PostMapping("cancelFollow")
    public ResultRO cancelFollow(@RequestBody @Valid @NotNull FollowAddVO addVO) {
        followService.cancelFollow(addVO);
        return new ResultRO<>();
    }
}
